package com.intelligent.system.common.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.intelligent.system.common.dto.MessageDTO;
import com.intelligent.system.common.dto.MessageQueryDTO;
import com.intelligent.system.common.response.ResultDO;
import com.intelligent.system.common.service.IMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@Api(tags = "消息管理")
@RestController
@RequestMapping("/api/message")
@Validated
public class MessageController {

    @Resource
    private IMessageService messageService;

    @ApiOperation("发送消息")
    @PostMapping("/send")
    public ResultDO<Void> sendMessage(
            @ApiParam(value = "消息信息", required = true)
            @RequestBody @Valid MessageDTO messageDTO) {
        log.info("发送消息请求参数: {}", messageDTO);
        return messageService.sendMessage(messageDTO);
    }

    @ApiOperation("发送广播消息")
    @PostMapping("/broadcast")
    public ResultDO<Void> sendBroadcast(
            @ApiParam(value = "广播消息信息", required = true)
            @RequestBody @Valid MessageDTO messageDTO) {
        log.info("发送广播消息请求参数: {}", messageDTO);
        return messageService.sendBroadcast(messageDTO);
    }

    @ApiOperation("获取消息详情")
    @GetMapping("/{id}")
    public ResultDO<MessageDTO> getMessage(
            @ApiParam(value = "消息ID", required = true)
            @PathVariable Long id) {
        return messageService.getMessage(id);
    }

    @ApiOperation("更新消息状态")
    @PutMapping("/{id}/status/{status}")
    public ResultDO<Void> updateStatus(
            @ApiParam(value = "消息ID", required = true)
            @PathVariable Long id,
            @ApiParam(value = "消息状态", required = true)
            @PathVariable Integer status) {
        return messageService.updateStatus(id, status);
    }

    @ApiOperation("删除消息")
    @DeleteMapping("/{id}")
    public ResultDO<Void> deleteMessage(
            @ApiParam(value = "消息ID", required = true)
            @PathVariable Long id) {
        return messageService.deleteMessage(id);
    }

    @ApiOperation("分页查询消息列表")
    @PostMapping("/page")
    public ResultDO<IPage<MessageDTO>> pageMessages(
            @ApiParam(value = "分页参数", required = true)
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @ApiParam(value = "查询条件")
            @RequestBody MessageQueryDTO queryDTO) {
        Page<MessageDTO> page = new Page<>(current, size);
        return messageService.pageMessages(page, queryDTO);
    }

    @ApiOperation("获取未读消息数量")
    @GetMapping("/unread/count")
    public ResultDO<Long> getUnreadCount(
            @ApiParam(value = "接收者ID", required = true)
            @RequestParam Long receiverId) {
        return messageService.getUnreadCount(receiverId);
    }

    @ApiOperation("获取用户的消息列表")
    @PostMapping("/user/{receiverId}")
    public ResultDO<List<MessageDTO>> getUserMessages(
            @ApiParam(value = "接收者ID", required = true)
            @PathVariable Long receiverId,
            @ApiParam(value = "查询条件")
            @RequestBody MessageQueryDTO queryDTO) {
        return messageService.getUserMessages(receiverId, queryDTO);
    }

    @ApiOperation("批量更新消息状态")
    @PutMapping("/batch/status")
    public ResultDO<Void> batchUpdateStatus(
            @ApiParam(value = "消息ID列表", required = true)
            @RequestBody List<Long> ids,
            @ApiParam(value = "消息状态", required = true)
            @RequestParam Integer status) {
        return messageService.batchUpdateStatus(ids, status);
    }

    @ApiOperation("批量删除消息")
    @DeleteMapping("/batch")
    public ResultDO<Void> batchDelete(
            @ApiParam(value = "消息ID列表", required = true)
            @RequestBody List<Long> ids) {
        return messageService.batchDelete(ids);
    }
} 